package com.cxl.demo.service;

import com.cxl.demo.mapper.*;
import com.cxl.demo.util.GetData;
import com.cxl.demo.util.RandomUtil;
import com.cxl.demo.util.TimeUpd;
import com.cxl.demo.vo.Goods;
import com.cxl.demo.vo.Pager;
import com.cxl.demo.vo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;
import java.util.Random;


@Service
public class UserServiceImpl implements UserService{

    @Autowired
    UserMapper userMapper;
    @Autowired
    AssigmentMapper assigmentMapper;

    @Autowired
    BuyHistoryMapper buyHistoryMapper;

    @Autowired
    RecordMapper recordMapper;

    @Autowired
    GoodsMapper  goodsMapper;

    @Override
    public User Login(Integer userId, String password) {
       User user= userMapper.findUserByUserIdAndPassword(userId,password);
        return user;
    }

    @Override
    public Integer register(User user) {
    //    Date date= new Date();
        String timestamp = new TimeUpd().getDate();

       user.setCreateTime(timestamp);
        Integer userId=0;
       user.setScore(100.0);
        Integer ok=0;

        userId=Integer.parseInt(new GetData().Data()+new RandomUtil().getFive());
        user.setUserId(userId);
        ok=userMapper.addUser(user);

        return userId;
    }

    @Override
    public Integer UpdateUser(User user) {
        userMapper.updateUser(user);
        return 1;
    }

    @Override
    public User GetUser(Integer userId) {
        return userMapper.findUserByUserId(userId);
    }

    @Override
    public Integer DelUser(Integer userId) {


    Integer ok= assigmentMapper.getUserAssigmentNum(userId);
   Integer ok2=buyHistoryMapper.findUserBuyHistoryNum(userId);
   Integer ok3=goodsMapper.GetUserGoodsNum(userId);
   Integer ok4=recordMapper.getRecordsByUserIdNum(userId);
   if(ok==0&&ok2==0&&ok3==0&&ok4==0){
       userMapper.delUser(userId);
       return 1;
   }else{
       return 0;
   }


    }

    @Override
    public Pager getBusiness(Map<String, Object> map) {


        Pager pager=new Pager();
        pager.setRows(userMapper.GetBusinessList(map));
        pager.setTotal(userMapper.GetBusinessNum());
        return pager;
    }


}
